package com.duan.luren.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <b>description</b>： Java高并发、微服务、性能优化实战案例100讲，视频号：程序员路人，源码 & 文档 & 技术支持，请加个人微信号：itsoku <br>
 * <b>time</b>：2024/5/7 14:51 <br>
 * <b>author</b>：ready likun_557@163.com
 */
@Configuration
public class RabbitMQConfiguration {
    /**
     * 订单的rabbitmq配置
     */
    @Configuration
    public static class Order {
        public static final String EXCHANGE = "orderExchange";
        public static final String QUEUE = "orderQueue";
        public static final String ROUTING_KEY = "create";

        @Bean
        public DirectExchange orderExchange() {
            // 创建具体类型的交换机
            // 四种AbstractExchange的实现类
            //DirectExchange(默认) 将消息路由到交换机绑定队列种指定key的队列内
            //FanoutExchange 将消息路由到与该交换机绑定的所有队列种，类似于广播
            //TopicExchange key的模糊匹配
            //HeadersExchange 根据消息头键值对匹配（非key）不常用，性能较差
            //CustomExchange 自定义路由规则
            return new DirectExchange(EXCHANGE, true, false);
        }

        @Bean
        public Queue orderQueue() {
            return new Queue(QUEUE, true);
        }

        @Bean
        public Binding orderExchangeBindOrderQueue() {
            return new Binding(QUEUE, Binding.DestinationType.QUEUE, EXCHANGE, ROUTING_KEY, null);
        }
    }

    /**
     * 用户的rabbitmq配置
     */
    @Configuration
    public static class User {
        public static final String EXCHANGE = "userExchange";
        public static final String QUEUE = "userQueue";
        public static final String ROUTING_KEY = "create";

        @Bean
        public FanoutExchange userExchange() {
            return new FanoutExchange(EXCHANGE, true, false);
        }

        @Bean
        public Queue userQueue() {
            return new Queue(QUEUE, true);
        }

        @Bean
        public Binding userExchangeBindOrderQueue() {
            return new Binding(QUEUE, Binding.DestinationType.QUEUE, EXCHANGE, ROUTING_KEY, null);
        }
    }

    /**
     * 用户的rabbitmq配置
     */
    @Configuration
    public static class User2 {
        public static final String EXCHANGE = "user2Exchange";
        public static final String QUEUE = "user2Queue";
        public static final String ROUTING_KEY = "create";

        @Bean
        public FanoutExchange user2Exchange() {
            return new FanoutExchange(EXCHANGE, true, false);
        }

        @Bean
        public Queue user2Queue() {
            return new Queue(QUEUE, true);
        }

        @Bean
        public Binding user2ExchangeBindOrderQueue() {
            return new Binding(QUEUE, Binding.DestinationType.QUEUE, EXCHANGE, ROUTING_KEY, null);
        }
    }
}
